package oocl.ita.eordering.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import oocl.ita.eordering.po.Client;
import oocl.ita.eordering.po.Food;
import oocl.ita.eordering.po.Merchant;
import oocl.ita.eordering.po.Order;
import oocl.ita.eordering.service.ClientService;
import oocl.ita.eordering.service.FoodService;
import oocl.ita.eordering.service.MerchantService;
import oocl.ita.eordering.util.InitClass;
import oocl.ita.eordering.vo.Result;
import oocl.ita.eordering.vo.Vfood;
import oocl.ita.eordering.webService.GetFoodRecommendWS;
import oocl.ita.eordering.webService.GetMerchantPassWS;

@Controller
public class ClientController {

	@Autowired(required = true)
	private ClientService clientService;
	@Autowired(required = true)
	private MerchantService merchantService;
	@Autowired(required = true)
	private FoodService foodService;
	// @Autowired
	// private InitClass initClass;
	@Autowired
	GetMerchantPassWS getMerchantPassWS;
	@Autowired
	GetFoodRecommendWS getFoodRecommendWS;

	// 用户登录
	@RequestMapping("/login")
	@ResponseBody
	public Result login(String username, String password, HttpServletRequest request) {

		Client c = clientService.clientLogin(username, password);
		if (c != null) {
			request.getSession().setAttribute("c", c);
			Result result = new Result();
			result.success("登录成功");
			return result;
		} else {
			// Result result = new Result(2, "error");
			Result result = new Result();
			result.fail("登录失败");
			return result;
		}

	}

	// 用户退出
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request) {
		request.getSession().invalidate();
		dataInit(request.getSession());
		return "redirect:/";
	}

	// 用户注册
	@RequestMapping(value = "/regist", method = RequestMethod.POST)
	@ResponseBody
	public Result regist(Client c, HttpServletRequest request) throws Exception {
		System.out.println("...person name....." + c.getcPersonName());
		Result result = new Result();
		// c.setcLocation(new String(c.getcLocation().getBytes("iso-8859-1")));
		System.out.println(c.getcLocation());
		System.out.println(c.getcPassword());
		System.out.println(c.getcTel());
		List<Client> clients = new ArrayList<Client>();
		for (Client client : clients) {
			if (c.getcTel() == client.getcTel()) {
				result.fail("输入号码有误");
				return result;
			}
		}
		int rs = clientService.clientRegist(c);
		if (rs > 0) {
			request.getSession().setAttribute("c", c);
			// Result result = new Result(1, "success");
			result.success("注册成功");
			return result;
		} else {
			// Result result = new Result(2, "error");
			result.fail("注册失败");
			return result;
		}
	}

	@RequestMapping(value = "/regist", method = RequestMethod.GET)
	public String regist(HttpServletRequest request) {
		return "register";
	}
	//
	// @RequestMapping("/showMechant")
	// public String showMechant(HttpServletRequest request) {
	// List<Merchant> merchants = merchantService.findAllMerchant();
	// request.getSession().setAttribute("merchants", merchants);
	// if (merchants != null) {
	// for (Merchant merchant : merchants) {
	// System.out.println(merchant.getmPersonName());
	// }
	// return "index";
	// } else {
	// return "error";
	// }
	// }

	// 跳转到食物列表页面
	@RequestMapping("/showFood")
	public String showFood(int mId, HttpServletRequest request) {
		Merchant merchant = merchantService.findMerchantById(mId);
		request.getSession().setAttribute("merchantInfo", merchant);
		List<Food> foods = foodService.findFoodByMerchantId(mId);
		request.getSession().setAttribute("foods", foods);
		if (foods != null) {
			for (Food food : foods) {
				System.out.println(food.getmId());
				System.out.println(food.getfId());
			}
		}
		return "food";
	}

	private void dataInit(HttpSession session) {
		List<Merchant> merchants = getMerchantPassWS.getMechantPass();
		session.getServletContext().setAttribute("merchants", merchants);
		for (Merchant merchant : merchants) {
			System.out.println(merchant.getmTel());
		}

		List<String> fidList = getFoodRecommendWS.getMechantPass();
		List<Vfood> fList = new ArrayList<Vfood>();
		for (String s : fidList) {
			Integer i = Integer.parseInt(s);
			System.out.println(foodService.findFoodById(i).getfName());
			Vfood v = new Vfood(foodService.findFoodById(i));
			fList.add(v);
		}
		session.getServletContext().setAttribute("fList", fList);
		System.out.println("recommend foods:....." + fList.size());

		System.out.println("Init.....Success!");
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/init")
	@ResponseBody
	public String init(HttpSession session) {
		List<Merchant> merchants = (List<Merchant>) session.getServletContext().getAttribute("merchants");
		if (merchants == null) {
			dataInit(session);
			return "success";
		}
		return null;
	}

	// 提供A端通过oId查询complaint信息的接口
	@RequestMapping("/merchantStatusChange")
	@ResponseBody
	public String complaintByOIdForAdmin(HttpSession session) {
		System.out.println("商家状态改变接口");
		dataInit(session);
		// System.out.println(order.getComplaint());
		return "success";
	}

}
